import * as API_Distribution from '../../api/distribution'
import { seller_id,shareMessage } from "../../config/config";
Page({
  data: {
    // webSrc: 'http://127.0.0.1:3008',
	// webSrc: 'http://192.168.0.47:3008' // 本地
    webSrc: 'https://h5.oupinguoji.cn', // 上传时跳至此默认
  hasOnShow: false,
  query: "",
  sharename:'',
  shareimg:'',
  },
  onLoad(options) {
    API_Distribution.getsharemessage(seller_id).then( res => {
      // console.log( res) 
      this.setData({
        sharename:res.shop_mini_title,
        shareimg:res.shop_mini_share
      })
    }).catch( err => {
      console.error( err)
    })
    if (this.data.hasOnShow) {
    return
  }
         // 上下级关系获取绑定
  if(options.su){
    API_Distribution.accessShortLink({ su: options.su }).then(res => {
      wx.setStorageSync('inviteCode',res.mobile)
    })
  }
	this.setData({
	    hasOnShow: true
	})
    console.log('首页的url地址', 'https://h5.oupinguoji.cn')
    this.setData({
		// webSrc: 'http://127.0.0.1:3008'
      webSrc: 'https://h5.oupinguoji.cn'// 上传时跳至此默认
	  // webSrc: 'http://192.168.0.47:3008' // 本地
    })
  },
  //刷新webview页面
  async onShow() {
    // let tmpUrl = this.data.webSrc;
    // this.setData({ webSrc: ''});
    // setTimeout(() => {this.setData({webSrc: tmpUrl})}, 10);
    if (wx.getStorageSync("access_token")){
    let response = await API_Distribution.generateShortLink();
    let query = response.message.substring(
      response.message.indexOf("?"),
      response.message.length
    );
    this.setData({
      query
    })
  }
  },
  
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    wx.showShareMenu({
      withShareTicket: true,
    });
    return {
      title:this.data.sharename,
      desc: this.data.sharename,
      path: '/pages/home/home' + this.data.query,
      imageUrl: this.data.shareimg
    }
  } 
})